package com.github.fashionbrot;

import com.github.fashionbrot.util.PermissionUtil;
import org.springframework.web.method.HandlerMethod;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.function.Predicate;

public class PermissionMethod {


    public static boolean isPermission(Object handler, Predicate<String[]> verify) {
        Method handlerMethod = getHandlerMethod(handler);
        return PermissionUtil.checkPermission(handlerMethod, verify);
    }


    public static Method getHandlerMethod(Object handler) {
        if (handler != null && handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            Method method = handlerMethod.getMethod();
            return method;
        }
        return null;
    }


    public static boolean isHtmlRequest(HttpServletRequest request) {
        String acceptHeader = request.getHeader("Accept");
        // 如果 Accept 头为空，默认视为 HTML 请求
        if (acceptHeader == null || acceptHeader.contains("text/html")) {
            return true;
        }
        // 如果是 JSON 请求，返回 false
        return acceptHeader.contains("application/json");
    }


}
